
//
//  WBPictureCollectionView.swift
//  微博(swift)
//
//  Created by 汤米 on 16/4/21.
//  Copyright © 2016年 汤米. All rights reserved.
//

import UIKit

private let WBPictureCollectionViewCellId = "WBPictureCollectionViewCellId"
private let itemMargin: CGFloat = 5
private let cellWH: CGFloat = (kScreenW - 20 - itemMargin * 2) / 3

class WBPictureCollectionView: UICollectionView {

    
    var pictures: [WBPictureModel]? {
        
        didSet {
            
                
                let size = setPictureSize(pictures?.count ?? 0)
                self.snp_updateConstraints { (make) -> Void in
                    
                    make.size.equalTo(size)
            }
            self.reloadData()
            
        }
    }
    
    // MARK: -  处理配图的Size
    func setPictureSize(pictureCount: Int) -> CGSize {
        
        let col = pictureCount >= 3 ? 3 : pictureCount
        let row = (pictureCount - 1) / 3 + 1
        
        // 配图的宽高
        let width = CGFloat(col) * cellWH + CGFloat(col - 1) * itemMargin

        let height = CGFloat(row) * cellWH + CGFloat(row - 1) * itemMargin
     
        return CGSize(width: width, height: height)

        
    }
    
    // MARK: -  重写 init 方法
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setUpUI()
       
    }
    
    // MARK: -  重写 init方法时必须要实现的方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: -  设置UI
    private func setUpUI() {
        
        self.backgroundColor = UIColor.clearColor()

        dataSource = self
        //注册cell
        registerClass(WBCustomPictureViewCell.self, forCellWithReuseIdentifier: WBPictureCollectionViewCellId)
        
        //设置流水布局
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
        
        layout.minimumInteritemSpacing = itemMargin
        layout.minimumLineSpacing = itemMargin
        
        
    }
}

// MARK: -  collectionView的数据源方法
extension WBPictureCollectionView: UICollectionViewDataSource {
    
    // MARK: -  设置item的个数
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return pictures?.count ?? 0
    }
    
    // MARK: -  设置item的样子
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(WBPictureCollectionViewCellId, forIndexPath: indexPath) as! WBCustomPictureViewCell
        
        cell.picture = pictures![indexPath.item]
        
        return cell
    }
    
    
}


// MARK: -  自定义配图cell
class WBCustomPictureViewCell: UICollectionViewCell {
    
    var picture: WBPictureModel? {
        
        didSet {
            picture?.thumbnail_pic
            pictureImageView.sd_setImageWithURL(NSURL(string: picture?.thumbnail_pic ?? ""), placeholderImage: UIImage(named: "timeline_card_top_background"))
            
            if let p = picture?.thumbnail_pic where p.hasSuffix(".gif") {
                
                GIFImageView.hidden = false
            }else{
                // 不是动态图片
                GIFImageView.hidden = true
            }
        }
    }
    
    // MARK: -  重写 init方法
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setUpUI()
    }

    // MARK: -  重写 init方法时必须要实现的方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: -  设置视图
    private func setUpUI() {
        
        self.backgroundColor = UIColor.whiteColor()
        
        addSubview(pictureImageView)
        addSubview(GIFImageView)
        
        pictureImageView.snp_makeConstraints { (make) -> Void in
            
            make.top.leading.bottom.trailing.equalTo(self)
        }
        
        GIFImageView.snp_makeConstraints { (make) -> Void in
            
            make.trailing.bottom.equalTo(pictureImageView)
        }
    }
    
    
    // MARK: -  懒加载控件
    //显示图片的imageView
    private lazy var pictureImageView: UIImageView = {
        
        let pictureImageView = UIImageView(image: UIImage(named: "timeline_card_top_background"))
        pictureImageView.contentMode = .ScaleAspectFill
        pictureImageView.clipsToBounds = true
        
        return pictureImageView
    }()
    
    // MARK: -  懒加载动图Logo
    private lazy var GIFImageView: UIImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
}

